﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ManageBusStaion
/// </summary>
public class ManageBusStaion
{

    public DataSet searchBusStationByKeyword(string key)
    {
        DataSet ds = new DataSet();
        SqlConnection con = ManageConnect.OpenConnection();
        try
        {
            SqlDataAdapter sqlDb = new SqlDataAdapter("searchBusStationByKey", con);
            sqlDb.SelectCommand.CommandType = CommandType.StoredProcedure;

            SqlParameter param = new SqlParameter("@STRING_SEARCH", SqlDbType.NVarChar);
            param.Value = key;
            sqlDb.SelectCommand.Parameters.Add(param);

            sqlDb.Fill(ds, "Stations");
        }
        catch (SqlException ex)
        {

        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }

        return ds;
    }

    public DataSet getAllBusStation()
    {
        DataSet ds = new DataSet();
        SqlConnection con = ManageConnect.OpenConnection();
        try
        {
            SqlDataAdapter da = new SqlDataAdapter("getAllBusStation", con.ConnectionString);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.Fill(ds, "Stations");
        }
        catch { }
        finally
        {
            ManageConnect.CloseConnection(con);
        }

        return ds;
    }

    public int insertNewBusStaion(BusStation station)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("insertNewBusStation", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@StationName", SqlDbType.NVarChar).Value = station.StaionName;
        cmd.Parameters.Add("@Longitude", SqlDbType.NVarChar).Value = station.Longitude;
        cmd.Parameters.Add("@Latitude", SqlDbType.NVarChar).Value = station.Latitude;

        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }

    public int deleteBusStation(BusStation station)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("deleteBusStationByID", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@IDStation", SqlDbType.Int).Value = station.IDStation;

        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }

    public int editBusStation(BusStation station)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("updateBusStation", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@IDStation", SqlDbType.Int).Value = station.IDStation;
        cmd.Parameters.Add("@StationName", SqlDbType.NVarChar).Value = station.StaionName;
        cmd.Parameters.Add("@Longitude", SqlDbType.NVarChar).Value = station.Longitude;
        cmd.Parameters.Add("@Latitude", SqlDbType.NVarChar).Value = station.Latitude;
        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }

}